<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

include(APPPATH . "controllers/base.php");

class Sitemap extends Base {

	function __construct() {
		parent::__construct();
		$this->load->model('sitemap/sitemap_model');
	}

	function index($url = '') {
		$cache_key = 'sitemap_index';
		$cache_time = 30*60;
		$cid = ''; //index
		$pid = ''; //province
		$did = ''; //dicstric
		$type = 1;
		$limit = 2000;
		$sitemap_data = mc_get($cache_key); //Cache

		if ($url!= '') {
			$row = $this->sitemap_model->get_from_tbl_route(str_replace('_', '-', $url));
			$cid = $row['typeid'];
			$type = $row['type'];
		}
		else {
			$this->_data['isHome'] = 1;
		}

		if ($sitemap_data == FALSE) {
			//type 3
			if ($type == 3) {
				$did = $cid;
				$cid = 0;
			}

			//type 2 -> Get list province
			if ($type == 2) {
				$lstDistric = $this->sitemap_model->get_distric_arr($cid);
				$pid = $cid;
				$cid = 0;
			}

			//type 1 -> Get list category
			if ($type == 1) {
				$lstCat = $this->sitemap_model->get_category_arr($cid);
			}

			//Get list announcenments
			$lstAnn = $this->sitemap_model->get_announcement_arr($cid, $pid, $did, $limit);
			$sitemap_data['tpl_file'] = 'sitemap/index';
			$sitemap_data['lstDistric'] = $lstDistric;
			$sitemap_data['lstCat'] = $lstCat;
			$sitemap_data['lstAnn'] = $lstAnn;
			mc_set($cache_key, $sitemap_data, $cache_time);
		}

		$this->_data = $sitemap_data;

		//Load to view
		#$this->output->enable_profiler(TRUE);
		$this->load->view('layout/single', $this->_data);
	}

	function project() {
		$cache_key = 'sitemap_project';
		$cache_time = 30*60;
		$sitemap_data = mc_get($cache_key);
		if ($sitemap_data == FALSE) {
			$sitemap_data['tpl_file'] = 'sitemap/index';
			$sitemap_data['lstProject'] = $this->sitemap_model->get_project();
			mc_set($cache_key, $sitemap_data, 30*60);
		}
		$this->_data = $sitemap_data;		
		#$this->output->enable_profiler(TRUE);
		$this->load->view('layout/single', $this->_data);
	}
}
